var mQrcodeOptions = {"text":"","correctLevel":QRCode.CorrectLevel.H,drawer:'canvas',width: 500,height: 500,colorLight:"#FFFFFF",quietZone:30,"AI":"#000000","AO":"#000000"};

$("#mQrcodeColor").spectrum({
    color: "#000000",
    cancelText: "取消",
    chooseText: "确定选择",
});
$("#mQrcodeBackgroundColor").spectrum({
    color: "#FFFFFF",
    cancelText: "取消",
    chooseText: "确定选择",
});
$("#mQrcodePO").spectrum({
    color: "#000000",
    cancelText: "取消",
    chooseText: "确定选择",
});
$("#mQrcodePI").spectrum({
    color: "#000000",
    cancelText: "取消",
    chooseText: "确定选择",
});

function clearSetting(argument) {
    var mQrcodeText = mQrcodeOptions.text;
    mQrcodeOptions = {"text":mQrcodeText,"correctLevel":QRCode.CorrectLevel.H,drawer:'canvas',width: 500,height: 500,"colorDark":"#000000",colorLight:"#FFFFFF",quietZone:30,"AI":"#000000","AO":"#000000"};
    mQrcodeGenerate();
}

function qrcodeChange(object) {
    var id = object.id;
    var value = object.value;
    if (id == "mQrcodeSize") {
        mQrcodeOptions.width = parseInt(value);
        mQrcodeOptions.height = parseInt(value);
    } else if (id == "mQrcodeText") {
        mQrcodeOptions.text = value;
    } else if (id == "mQrcodeCorrectLevel") {
        if (value == "L") {
            mQrcodeOptions.correctLevel = QRCode.CorrectLevel.L;
        } else if (value == "M") {
            mQrcodeOptions.correctLevel = QRCode.CorrectLevel.M;
        } else if (value == "Q") {
            mQrcodeOptions.correctLevel = QRCode.CorrectLevel.Q;
        } else {
            mQrcodeOptions.correctLevel = QRCode.CorrectLevel.H;
        }
    } else if (id == "mQrcodeDotScale") {
        mQrcodeOptions.dotScale = parseFloat(value);
        mQrcodeOptions.dotScaleTiming = parseFloat(value);
    } else if (id == "mQrcodeColor") {
        mQrcodeOptions.colorDark = value;
        mQrcodeOptions.AI = value;
        mQrcodeOptions.AO = value;
    } else if (id == "mQrcodeBackgroundColor") {
        mQrcodeOptions.colorLight = value;
        mQrcodeOptions.quietZoneColor = value;
    } else if (id == "mQrcodeQuietZone") {
        mQrcodeOptions.quietZone = mQrcodeOptions.width/100*3*value;
    } else if (id == "mQrcodePO") {
        mQrcodeOptions.PO = value;
    } else if (id == "mQrcodePI") {
        mQrcodeOptions.PI = value;
    }
    mQrcodeGenerate();
}

function mQrcodeGenerate() {
    $("#mQrcodePreview").html("");
    new QRCode(document.getElementById("mQrcodePreview"), mQrcodeOptions);
}

function choiceQrcodeLogo() {
    var $file = event.currentTarget;
    var src = getObjectURL($file.files[0]);
    mQrcodeOptions.logo = src;
    mQrcodeGenerate();
}

function getObjectURL(file) {
    var url = null ;
    if (window.createObjectURL!=undefined) {
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { 
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { 
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}

$("#mqrcodeRecommend img").click(function(){
    var src = $(this).attr("src"); 
    mQrcodeOptions.logo = src;
    mQrcodeGenerate();
});

function downloadQrcode() {
    const canvas = document.querySelector("#mQrcodePreview canvas");
    console.log(canvas);
    const el = document.createElement('a');
    el.href = canvas.toDataURL('image/jpeg');
    el.download = 'qrcode.jpg';
    const event = new MouseEvent('click');
    el.dispatchEvent(event);
}